package io.renren.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.dao.EmpArchivesbaseDao;
import io.renren.dao.PersonDispatchDao;
import io.renren.entity.EmpArchivesbaseEntity;
import io.renren.entity.PersonDispatchEntity;
import io.renren.entity.SysUserEntity;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.dao.EmpArchivesbaseRecordDao;
import io.renren.entity.EmpArchivesbaseRecordEntity;
import io.renren.service.EmpArchivesbaseRecordService;



@Service("empArchivesbaseRecordService")
public class EmpArchivesbaseRecordServiceImpl implements EmpArchivesbaseRecordService {
	@Autowired
	private EmpArchivesbaseRecordDao empArchivesbaseRecordDao;

	@Autowired
	private EmpArchivesbaseDao empArchivesbaseDao;

	@Autowired
	private PersonDispatchDao personDispatchDao;
	
	@Override
	public EmpArchivesbaseRecordEntity queryObject(Integer id){
		return empArchivesbaseRecordDao.queryObject(id);
	}
	
	@Override
	public List<EmpArchivesbaseRecordEntity> queryList(Map<String, Object> map){
		return empArchivesbaseRecordDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return empArchivesbaseRecordDao.queryTotal(map);
	}
	
	@Override
	public void save(EmpArchivesbaseRecordEntity empArchivesbaseRecord){
		empArchivesbaseRecordDao.save(empArchivesbaseRecord);
	}

	@Override
	public void update(EmpArchivesbaseRecordEntity empArchivesbaseRecord){
		empArchivesbaseRecordDao.update(empArchivesbaseRecord);
	}
	
	@Override
	public void delete(Integer id){
		empArchivesbaseRecordDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		empArchivesbaseRecordDao.deleteBatch(ids);
	}

	@Override
	public void passBatch(Integer[] ids) {
		empArchivesbaseRecordDao.passBatch(ids);

	}

	@Override
	public void pass(Integer id) {
		empArchivesbaseRecordDao.passById(id);
	}

	@Override
	public String archConfirm(JSONObject jsonObject) {
		StringBuffer res = new StringBuffer();
		String idss = jsonObject.getString("ids");
		String[] ids = idss.substring(1,idss.length()-1).split(",");
		SysUserEntity user = (SysUserEntity)jsonObject.get("user");
		for (Object id : ids) {
			id = id.toString().substring(1, id.toString().length() - 1);

			PersonDispatchEntity person = personDispatchDao.queryObject(id);
			EmpArchivesbaseEntity archbase = empArchivesbaseDao.getRecordByPersonid(person.getPersonId());
			if(archbase!=null){
				EmpArchivesbaseRecordEntity recordEntity = new EmpArchivesbaseRecordEntity();
				recordEntity.setEmpArchivesbaseId(archbase.getId());
				recordEntity.setArchivesbaseCategory(jsonObject.getString("archivesbaseCategory"));
				recordEntity.setOperation(jsonObject.getString("operation"));
				recordEntity.setOperator(user.getUsername());
				recordEntity.setOperatorDate(new Date());
				recordEntity.setCheckState("0");

				empArchivesbaseRecordDao.saveByDynamicCol(recordEntity);
			}else{
				res.append(person.getPersonId()+",");
			}
		}
		if(!StringUtils.isBlank(res))
			return res.substring(0,res.length()-1);
		else
			return null;
	}


}
